package view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import model.interfaces.IPacman;


public class DrawingArea extends JPanel{
	private static final long serialVersionUID = 1L;
	private boolean debugModeOn;
	private Image bg;
	private ArrayList<Drawable> objectsToDraw;
	
	// initializes a panel and a label
	// links the panel to the mouseListener
	public DrawingArea(String bgImg, int minX,int minY,boolean debugMode) {
		objectsToDraw = new ArrayList<Drawable>();
		setMinimumSize(new Dimension(minX,minY));
		debugModeOn = debugMode;
		bg = new ImageIcon(bgImg).getImage();
		
		if(debugModeOn)
			System.out.printf("tam: %d,%d\n",getWidth(), getHeight());
	}
	
	// adds a new object
	public void addObject(Drawable object){
		objectsToDraw.add(object);
		repaint();
	}
		
	// paints backgaddConnectionround and 
	public void paint(Graphics g){
		g.drawImage (bg, 0, 0, getWidth(), getHeight(), null);
		for(Drawable drawable : objectsToDraw){
			drawable.draw(g);			
		}
	}
	
	// sets movment and changes packman animation acording to key
	public void updatePack(char key){
		PackGraphics graph = (PackGraphics) objectsToDraw.get(0);
		IPacman pack = graph.getPack();
		
		switch(key){
			case 'w':
				graph.changeState(0);
				pack.movimientoArriba();
				break;
			case 's':
				graph.changeState(1);
				pack.movimientoAbajo();
				break;
			case 'a':
				graph.changeState(2);
				pack.movimientoAIzquierda();
				break;
			case 'd':
				graph.changeState(3);
				pack.movimientoADerecha();
				break;			
		}
	}
}
